# Maintainer: Alexey Pavlov <Alexpux@gmail.com>
# Contributor: Zach Bacon <11doctorwhocanada@gmail.com>
# Contributor: wirx6 <wirx654@gmail.com>
# Contributor: Ray Donnelly <mingw.android@gmail.com>

_realname=ffmpeg
pkgbase="mingw-w64-${_realname}"
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=6.0
pkgrel=7
pkgdesc="Complete solution to record, convert and stream audio and video (mingw-w64)"
arch=('any')
mingw_arch=('mingw32' 'mingw64' 'ucrt64' 'clang64' 'clang32' 'clangarm64')
url="https://ffmpeg.org/"
license=('spdx:GPL-3.0-or-later')
depends=("${MINGW_PACKAGE_PREFIX}-aom"
         "${MINGW_PACKAGE_PREFIX}-bzip2"
         "${MINGW_PACKAGE_PREFIX}-frei0r-plugins"
         "${MINGW_PACKAGE_PREFIX}-fribidi"
         "${MINGW_PACKAGE_PREFIX}-fontconfig"
         "${MINGW_PACKAGE_PREFIX}-dav1d"
         "${MINGW_PACKAGE_PREFIX}-gmp"
         "${MINGW_PACKAGE_PREFIX}-gnutls"
         "${MINGW_PACKAGE_PREFIX}-gsm"
         "${MINGW_PACKAGE_PREFIX}-lame"
         "${MINGW_PACKAGE_PREFIX}-libass"
         "${MINGW_PACKAGE_PREFIX}-libbluray"
         "${MINGW_PACKAGE_PREFIX}-libcaca"
         "${MINGW_PACKAGE_PREFIX}-libexif"
         "${MINGW_PACKAGE_PREFIX}-libgme"
         "${MINGW_PACKAGE_PREFIX}-libiconv"
         $([[ ${MINGW_PACKAGE_PREFIX} == *-clang-aarch64* ]] || echo "${MINGW_PACKAGE_PREFIX}-libmfx")
         "${MINGW_PACKAGE_PREFIX}-libmodplug"
         "${MINGW_PACKAGE_PREFIX}-libssh"
         "${MINGW_PACKAGE_PREFIX}-libplacebo"
         "${MINGW_PACKAGE_PREFIX}-librsvg"
         "${MINGW_PACKAGE_PREFIX}-libsoxr"
         "${MINGW_PACKAGE_PREFIX}-libtheora"
         "${MINGW_PACKAGE_PREFIX}-libvorbis"
         "${MINGW_PACKAGE_PREFIX}-libvpx"
         "${MINGW_PACKAGE_PREFIX}-libwebp"
         "${MINGW_PACKAGE_PREFIX}-libxml2"
         "${MINGW_PACKAGE_PREFIX}-openal"
         "${MINGW_PACKAGE_PREFIX}-opencore-amr"
         "${MINGW_PACKAGE_PREFIX}-openjpeg2"
         "${MINGW_PACKAGE_PREFIX}-opus"
         "${MINGW_PACKAGE_PREFIX}-rav1e"
         "${MINGW_PACKAGE_PREFIX}-rtmpdump"
         "${MINGW_PACKAGE_PREFIX}-SDL2"
         "${MINGW_PACKAGE_PREFIX}-speex"
         "${MINGW_PACKAGE_PREFIX}-srt"
          $([[ "${CARCH}" == "i686" ]] || echo "${MINGW_PACKAGE_PREFIX}-svt-av1")
         "${MINGW_PACKAGE_PREFIX}-vid.stab"
         "${MINGW_PACKAGE_PREFIX}-vulkan"
         "${MINGW_PACKAGE_PREFIX}-libx264"
         "${MINGW_PACKAGE_PREFIX}-x265"
         "${MINGW_PACKAGE_PREFIX}-xvidcore"
         "${MINGW_PACKAGE_PREFIX}-zimg"
         "${MINGW_PACKAGE_PREFIX}-zlib")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-autotools"
             "${MINGW_PACKAGE_PREFIX}-pkgconf"
             "${MINGW_PACKAGE_PREFIX}-dlfcn"
             $([[ ${MINGW_PACKAGE_PREFIX} == *-clang-aarch64* ]] || echo "${MINGW_PACKAGE_PREFIX}-amf-headers")
             $([[ ${MINGW_PACKAGE_PREFIX} == *-clang-aarch64* ]] || echo "${MINGW_PACKAGE_PREFIX}-ffnvcodec-headers")
             $([[ ${MINGW_PACKAGE_PREFIX} == *-clang-aarch64* ]] || echo "${MINGW_PACKAGE_PREFIX}-nasm"))
source=(https://ffmpeg.org/releases/${_realname}-${pkgver}.tar.xz{,.asc}
        "pathtools.c"
        "pathtools.h"
        "0005-Win32-Add-path-relocation-to-frei0r-plugins-search.patch"
        "0002-gcc-12.patch"
        "0001-lavu-vulkan-fix-handle-type-for-32-bit-targets.patch"
        https://github.com/FFmpeg/FFmpeg/commit/f9620d74cd49c35223304ba41e28be6144e45783.patch
        https://github.com/FFmpeg/FFmpeg/commit/effadce6c756247ea8bae32dc13bb3e6f464f0eb.patch)
validpgpkeys=('FCF986EA15E6E293A5644F10B4322F04D67658D8')
sha256sums=('57be87c22d9b49c112b6d24bc67d42508660e6b718b3db89c44e47e289137082'
            'SKIP'
            '08209cbf1633fa92eae7e5d28f95f8df9d6184cc20fa878c99aec4709bb257fd'
            '965d3921ec4fdeec94a2718bc2c85ce5e1a00ea0e499330a554074a7ae15dfc6'
            '58f91fde8be7fa093b13275c37b0276de08537449749599e5a50f4f9c8b20926'
            '84b9fcaa188eef15201a105a44c53d647e4fb800a5032336e0948d6bed2cbc1b'
            'bbc7e7b91886a8ac037d8e70692186ee4b48c37b4f1d82b81af8a54463b24803'
            '146d397029d8980224c0e805646bc02707b54029fff8a3e62c1565672091aabc'
            '206f4d8437b21f6197ffc444c86d0504892a5c2137cb227b4af1c1e8bf2c426c')

# Helper macros to help make tasks easier #
apply_patch_with_msg() {
  for _fname in "$@"
  do
    msg2 "Applying ${_fname}"
    patch -Nbp1 -i "${srcdir}"/${_fname}
  done
}

prepare() {
  test ! -d "${startdir}/../mingw-w64-pathtools" || {
    cmp "${startdir}/../mingw-w64-pathtools/pathtools.c" "${srcdir}/pathtools.c" &&
    cmp "${startdir}/../mingw-w64-pathtools/pathtools.h" "${srcdir}/pathtools.h"
  } || exit 1

  cd "${srcdir}/${_realname}-${pkgver}"
  cp -fHv "${srcdir}"/pathtools.[ch] libavfilter/

  apply_patch_with_msg \
    0005-Win32-Add-path-relocation-to-frei0r-plugins-search.patch \
    0002-gcc-12.patch

  # https://trac.ffmpeg.org/ticket/9983
  # https://ffmpeg.org/pipermail/ffmpeg-devel/2023-March/307142.html
  apply_patch_with_msg 0001-lavu-vulkan-fix-handle-type-for-32-bit-targets.patch

  apply_patch_with_msg f9620d74cd49c35223304ba41e28be6144e45783.patch

  # https://github.com/msys2/MINGW-packages/issues/17946
  apply_patch_with_msg effadce6c756247ea8bae32dc13bb3e6f464f0eb.patch
}

build() {
  local -a common_config
  common_config+=(
    --disable-debug
    --disable-stripping
    --disable-doc
    --enable-dxva2
    --enable-d3d11va
    --enable-fontconfig
    --enable-frei0r
    --enable-gmp
    --enable-gnutls
    --enable-gpl
    --enable-iconv
    --enable-libaom
    --enable-libass
    --enable-libbluray
    --enable-libcaca
    --enable-libdav1d
    --enable-libfreetype
    --enable-libfribidi
    --enable-libgme
    --enable-libgsm
    --enable-libmodplug
    --enable-libmp3lame
    --enable-libopencore_amrnb
    --enable-libopencore_amrwb
    --enable-libopenjpeg
    --enable-libopus
    --enable-libplacebo
    --enable-librsvg
    --enable-librtmp
    --enable-libssh
    --enable-libsoxr
    --enable-libspeex
    --enable-libsrt
    --enable-libtheora
    --enable-libvidstab
    --enable-libvorbis
    --enable-libx264
    --enable-libx265
    --enable-libxvid
    --enable-libvpx
    --enable-libwebp
    --enable-libxml2
    --enable-libzimg
    --enable-openal
    --enable-pic
    --enable-postproc
    --enable-runtime-cpudetect
    --enable-swresample
    --enable-version3
    --enable-vulkan
    --enable-zlib
    --enable-librav1e
  )

  if [[ "${CARCH}" != "i686" ]]; then
    common_config+=(
       --enable-libsvtav1
    )
  fi

  if [[ "${MINGW_PACKAGE_PREFIX}" != *clang-aarch64* ]]; then
    common_config+=(
        --enable-libmfx
        --enable-amf
        --enable-nvenc
    )
  fi

  for _variant in -static -shared; do
    [[ -d "${srcdir}/build-${MSYSTEM}${_variant}" ]] && rm -rf "${srcdir}/build-${MSYSTEM}${_variant}"
    mkdir -p "${srcdir}/build-${MSYSTEM}${_variant}" && cd "${srcdir}/build-${MSYSTEM}${_variant}"
    if [[ ${_variant} == -static ]]; then
      ENABLE_VARIANT="--enable-static --pkg-config-flags=--static"
    else
      ENABLE_VARIANT=--enable-shared
    fi
    ../${_realname}-${pkgver}/configure \
      --prefix=${MINGW_PREFIX} \
      --target-os=mingw32 \
      --arch=${CARCH%%-*} \
      --cc=${CC} \
      --cxx=${CXX} \
      "${common_config[@]}" \
      --logfile=config.log \
      ${ENABLE_VARIANT}

    make
  done
}

check() {
  for _variant in -static -shared; do
    cd "${srcdir}/build-${MSYSTEM}${_variant}"
    # workaround for conflict with SDL main(), use it if you have SDL installed
    # make check CC_C="-c -Umain"
    make check || true
  done
}

package() {
  for _variant in -static -shared; do
    cd "${srcdir}/build-${MSYSTEM}${_variant}"
    make DESTDIR="${pkgdir}" install
  done
  
  rm -f ${pkgdir}/${MINGW_PREFIX}/lib/*.def
  rm -f ${pkgdir}/${MINGW_PREFIX}/bin/*.lib

  local PREFIX_DEPS=$(cygpath -am ${MINGW_PREFIX})
  find ${pkgdir}${MINGW_PREFIX}/lib/pkgconfig -name *.pc -exec sed -i -e"s|${PREFIX_DEPS}|${MINGW_PREFIX}|g" {} \;
}
